package com.leyou.item.web;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupsByCid(@PathVariable("cid") Long cid){
        return ResponseEntity.ok(this.specificationService.queryGroupsByCid(cid));
    }

    @PutMapping("group")
    public ResponseEntity<Void> updateSpecGroupName(@RequestBody SpecGroup specGroup){
        this.specificationService.updateSpecGroupName(specGroup);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PostMapping("group")
    public ResponseEntity<Void> addSpecGroupName(@RequestBody SpecGroup specGroup){
        this.specificationService.addSpecGroupName(specGroup.getCid(), specGroup.getName());
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping("group/{id}")
    public ResponseEntity<Void> deleteSpecGroup(@PathVariable("id") String id){
        this.specificationService.deleteSpecGroup(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> selectSpecParam(
                         @RequestParam(value = "gid", required = false) Long gid,
                         // 考虑到以后可能还会根据是否搜索、是否为通用属性等条件过滤
                         @RequestParam(value = "cid", required = false)Long cid,
                         @RequestParam(value = "generic", required = false)Boolean generic,
                         @RequestParam(value = "searching", required = false)Boolean searching
    ){
        return ResponseEntity.ok(this.specificationService.selectSpecParam(gid, cid, generic, searching));
    }

    @PostMapping("param")
    public ResponseEntity<Void> addSpecParam(@RequestBody SpecParam specParam){
        this.specificationService.addSpecParam(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParam specParam){
        this.specificationService.updateSpecParam(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping("param/{id}")
    public ResponseEntity<Void> deleteSpecParam(@PathVariable("id") Long id){
        this.specificationService.deleteSpecParam(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecsByCid(@PathVariable("cid") Long cid){
        return ResponseEntity.ok(this.specificationService.querySpecsByCid(cid));
    }

}
